package stu.kurs.services.impl;


import java.util.List;

import javax.ejb.EJB;
import javax.ejb.Stateless;
import javax.persistence.TypedQuery;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;

import stu.kurs.domain.Stavki;
import stu.kurs.exceptions.ServiceException;
import stu.kurs.services.IMatchService;
import stu.kurs.services.IStavkiService;
import stu.kurs.services.IUserService;
import stu.kurs.services.impl.GenericService;


@Stateless(name="StavkiService")
public class StavkiService extends GenericService<Stavki> implements IStavkiService {


	@EJB(beanName="UserService")
	private IUserService UserService;
	

	@EJB(beanName="MatchService")
	private IMatchService MatchService;
	
	public StavkiService() {
		super(Stavki.class);
		// TODO Auto-generated constructor stub
	}


	@Override
	public void add(Integer many, Integer stavka, Integer iduser, Integer idmatch) throws ServiceException {
		Stavki stavki = new Stavki(many, stavka);
		 stavki.setUser(UserService.findById(iduser));
		 stavki.setMatch(MatchService.findById(idmatch));
		 save(stavki);
		
	}


	@Override
	public void delete(Integer id) throws ServiceException {
		Stavki stavki = findById(id);
		delete(stavki);
		
	}


	@Override
	public void changeStatus(Integer status, Integer id)	throws ServiceException {
		Stavki stavki = findById(id);
		stavki.setStatus(status);
		save(stavki);
		
	}

	@Override
	public List<Stavki> getAllStavki() throws ServiceException {
		List<Stavki> result = null;
		CriteriaBuilder cb = em.getCriteriaBuilder();
		CriteriaQuery<Stavki> cq = cb.createQuery(Stavki.class);
		Root<Stavki> stavki = cq.from(Stavki.class);
		cq.orderBy(cb.asc(stavki.get("many")));
		cq.orderBy(cb.asc(stavki.get("stavka")));
		cq.orderBy(cb.asc(stavki.get("status")));
		TypedQuery<Stavki> q = em.createQuery(cq); 
		try{
			result = q.getResultList();
		}
		catch (Exception e){
			throw new ServiceException(e);
		} 
		return result;
	}
    
	@Override
	public List<Stavki> getAllByStatus(Integer status) throws ServiceException {
		List<Stavki> result = null;
		CriteriaBuilder cb = em.getCriteriaBuilder();
		CriteriaQuery<Stavki> cq =cb.createQuery(Stavki.class);
		Root<Stavki> stavki = cq.from(Stavki.class);
		cq.where(cb.and(
				(cb.equal(stavki.get("status"), status))
				));
		cq.orderBy(cb.asc(stavki.get("many")));
		TypedQuery<Stavki> q = em.createQuery(cq); 
		try{
			result = q.getResultList();
		}
		catch (Exception e){
			throw new ServiceException(e);
		} 
		return result;
	}
	
	
	@Override
	public List<Stavki> getAllByUserId(Integer iduser) throws ServiceException {
		List<Stavki> result = null;
		CriteriaBuilder cb = em.getCriteriaBuilder();
		CriteriaQuery<Stavki> cq = cb.createQuery(Stavki.class);
		Root<Stavki> stavki = cq.from(Stavki.class);
		cq.where(cb.equal(stavki.get("user").get("id"), iduser));
		cq.orderBy(cb.asc(stavki.get("stavka")));
	    cq.orderBy(cb.asc(stavki.get("status")));
		TypedQuery<Stavki> q = em.createQuery(cq); 
		try{
			result = q.getResultList();
		}
		catch (Exception e){
			throw new ServiceException(e);
		} 
		return result;
	
	}


	@Override
	public List<Stavki> getAllByMatchId(Integer idmatch) throws ServiceException {
		List<Stavki> result = null;
		CriteriaBuilder cb = em.getCriteriaBuilder();
		CriteriaQuery<Stavki> cq = cb.createQuery(Stavki.class);
		Root<Stavki> stavki = cq.from(Stavki.class);
		cq.where(cb.equal(stavki.get("match").get("id"), idmatch));
	    cq.orderBy(cb.asc(stavki.get("stavka")));
	    cq.orderBy(cb.asc(stavki.get("status")));
		TypedQuery<Stavki> q = em.createQuery(cq); 
		try{
			result = q.getResultList();
		}
		catch (Exception e){
			throw new ServiceException(e);
		} 
		return result;
	}

	@Override
	public List<Stavki> getAllByStatusIdMatch(Integer status, Integer idmatch) throws ServiceException {
		List<Stavki> result = null;
		CriteriaBuilder cb = em.getCriteriaBuilder();
		CriteriaQuery<Stavki> cq =cb.createQuery(Stavki.class);
		Root<Stavki> stavki = cq.from(Stavki.class);
		cq.where(cb.and(
				(cb.equal(stavki.get("status"), status)),
				(cb.equal(stavki.get("match").get("id"), idmatch))
				));
		cq.orderBy(cb.asc(stavki.get("many")));
		TypedQuery<Stavki> q = em.createQuery(cq); 
		try{
			result = q.getResultList();
		}
		catch (Exception e){
			throw new ServiceException(e);
		} 
		return result;
	}


	@Override
	public List<Stavki> getAllByStatusIdUser(Integer status, Integer iduser) throws ServiceException {
		List<Stavki> result = null;
		CriteriaBuilder cb = em.getCriteriaBuilder();
		CriteriaQuery<Stavki> cq =cb.createQuery(Stavki.class);
		Root<Stavki> stavki = cq.from(Stavki.class);
		cq.where(cb.and(
				(cb.equal(stavki.get("status"), status)),
				(cb.equal(stavki.get("user").get("id"), iduser))
				));
		cq.orderBy(cb.asc(stavki.get("many")));
		TypedQuery<Stavki> q = em.createQuery(cq); 
		try{
			result = q.getResultList();
		}
		catch (Exception e){
			throw new ServiceException(e);
		} 
		return result;
	}
	
	






	
	
}
